package com.ssfGame.common
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	/**
	 * 
	 * @author lilh
	 * 
	 */	
	public class MouseCursor
	{
		private static var _stage:Stage;
		private static var _cursor:DisplayObject;
		private static var _container:Sprite;
		private static var _offX:int = 0;
		private static var _offY:int = 0;
		private static var _layer:Sprite;
		private static var _hideMouse:Boolean;
		
		public function MouseCursor()
		{
		}
		
		private static function addEvent():void
		{
			_stage.addEventListener(MouseEvent.MOUSE_MOVE, updateCursor);
		}
		
		private static function removeEvent():void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_MOVE, updateCursor);
		}
		
		private static function onMouseLeave(e:Event):void
		{
			Mouse.show();
		}
		
		private static function onMouseOver(e:Event):void
		{
			if(_hideMouse)
				Mouse.hide();
		}
		
		private static function updateCursor(e:MouseEvent = null) : void
		{
			_cursor.x = _layer.mouseX + _offX;
			_cursor.y = _layer.mouseY + _offY;
			if(e)
				e.updateAfterEvent();
			if(!_container || !e.target)
				return;
			if(_container.contains(e.target as DisplayObject))
			{
				_cursor.visible = true;
				if(_hideMouse)
					Mouse.hide();
			}else
			{
				_cursor.visible = false;
				Mouse.show();
			}	
		}
		
		public static function init(layer:Sprite, stage:Stage) : void
		{
			_layer = layer;
			_stage = stage;
		}
		/**
		 * 显示自定义光标
		 * cursor：光标样式
		 * offX：offY： 鼠标偏移量
		 * container 在此容器上显示鼠标样式 其他为默认鼠标
		 * 
		 */	
		public static function showMouseCursor(cursor:DisplayObject, offX:int = 0, offY:int = 0, container:Sprite = null, hideMouse:Boolean = true) : void
		{
			hideMouseCursor();
			_cursor = cursor;
			_hideMouse = hideMouse;
			_layer.addChild(_cursor);
			_offX = offX;
			_offY = offY;
			updateCursor();
			addEvent();
			if(_hideMouse)
				Mouse.hide();
			_container = container;
		}
		/**
		 * 删除自定义光标，显示默认光标
		 * 
		 */		
		public static function hideMouseCursor() : void
		{
			if (_cursor && _cursor.parent == _layer)
			{
				_layer.removeChild(_cursor);
			}
			Mouse.show();
			_cursor = null;
			removeEvent();
			_container = null;
		}
		
		
	}
}
